package org.david.code.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeTools {

	public static final String dateFormater = "yyyy-MM-dd";
	public static final String dateFormaterNoSplit = "yyyyMMdd";
	public static final String dateTimeFormater = "yyyy-MM-dd HH:mm:ss";
	
	private DateTimeTools() {
	}
	
	/**
	 * 按指定格式取得当前日期字符串
	 * 
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static final String getDateStringByPattern(String pattern) {
		Date date = new Date();
		return getDateStringByPattern(date, pattern);
	}

	/**
	 * 按指定格式取得当前日期类型
	 * 
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static final Date getCurrentDate(String pattern) {
		Date date = new Date();
		String dateStr = getDateStringByPattern(date, pattern);
		return getDateByPattern(dateStr, pattern);
	}
	
	public static final Calendar getCurrentCalendar(){
		return Calendar.getInstance();
	}

	/**
	 * 将日期按照指定格式进行格式化
	 * 
	 * @param date
	 *            日期
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static final String getDateStringByPattern(Date date, String pattern) {
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		String str = sf.format(date);
		return str;
	}

	/**
	 * 将时间戳按照指定格式进行格式化
	 * 
	 * @param strts
	 *            日期字符串
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static final String getTimestampStringByPattern(String strts, String pattern) {
		Timestamp ts = Timestamp.valueOf(strts);
		return getDateStringByPattern(ts, pattern);
	}

	/**
	 * 将时间戳按照指定格式进行格式化
	 * 
	 * @param ts
	 *            时间戳
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static final String getDateStringByPattern(Timestamp ts, String pattern) {
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		String str = sf.format(ts);
		return str;
	}

	/**
	 * 将时间戳转换为日期字符串
	 * 
	 * @param ts
	 *            时间戳
	 * @return
	 */
	public static final String getDateTimeString(Timestamp ts) {
		if (ts == null)
			return "";
		String str = ts.toString();
		if (str.length() >= 19)
			str = str.substring(0, 19);
		return str;
	}

	/**
	 * 取得当前时间戳
	 * 
	 * @return
	 */
	public static final Timestamp getDateTime() {
		Date date = new Date();
		return new Timestamp(date.getTime());
	}

	/**
	 * 取得当前时间戳的字符串
	 * 
	 * @return
	 */
	public static final String getDateTimeString() {
		Timestamp ts = getDateTime();
		return getDateTimeString(ts);
	}

	/**
	 * 取得当前时间戳中的日期部分
	 * 
	 * @return
	 */
	public static final String getDateString() {
		Timestamp ts = getDateTime();
		String str = getDateTimeString(ts);
		if (str.length() >= 10)
			str = str.substring(0, 10);
		return str;
	}

	/**
	 * 将日期字符串转化为时间戳类型
	 * 
	 * @param sDt
	 * @return
	 */
	public static final Timestamp getDateTime(String sDt) {
		if (sDt == null || "".equals(sDt.trim()))
			return null;
		try {
			return Timestamp.valueOf(sDt);
		} catch (IllegalArgumentException iae) {
			sDt = sDt + " 00:00:00";
		}
		return Timestamp.valueOf(sDt);
	}

	/**
	 * 将日期字符串转化为Date类型
	 * 
	 * @param strDate
	 *            日期字符串
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static final Date getDateByPattern(String strDate, String pattern) {
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = sf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 获得指定步长的日期，
	 * @param date	基础日期
	 * @param separator	分隔符
	 * @param stepDays	日期步长
	 * @return 例如：getStepDate("2008-08-04", "-", -6)，返回2008-07-29
	 */
	public static final String getStepDate(String date, String separator, int stepDays) {
		if (date == null || date.equals("") || date.length() != 10) {
			return date;
		}
		String[] str = date.split(separator);
		String year = str[0];
		String month = str[1];
		String day = str[2];
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, Integer.parseInt(year));
		cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		cal.set(Calendar.DATE, Integer.parseInt(day));
		cal.add(Calendar.DATE, stepDays);
		return cal.get(Calendar.YEAR) + separator
				+ ((cal.get(Calendar.MONTH) + 1) < 10 ? "0" + (cal.get(Calendar.MONTH) + 1) : (cal.get(Calendar.MONTH) + 1)) + separator
				+ (cal.get(Calendar.DATE) < 10 ? "0" + cal.get(Calendar.DATE) : cal.get(Calendar.DATE));
	}
	
	public static final Date getStepDate(Date date, int stepDays) {
		if( date==null ){
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, stepDays);
		return cal.getTime();
	}
	
	public static final Date getStepSecond(Date date, int stepSeconds) {
		if( date==null ){
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, stepSeconds);
		return cal.getTime();
	}
	
	public static final int getLeaveSec(long start, long end, int lenght) {
		int time = lenght - (int)(end - start)/1000;
		if(time > 0) {
			return time;
		} else {
			return 0;
		}
	}
	
	public static final int marginCalculations(Date start, Date end){
		long startTime = start.getTime();
		long endTime = end.getTime();
		int val = (int)(endTime - startTime)/1000;
		return val;
	}
	
	public static final Date getTodayClock(int hour)
	{
		Date today = new Date();
		SimpleDateFormat sf = new SimpleDateFormat(dateFormater);
		try {
			today = sf.parse(sf.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date(today.getTime() + hour * 60 * 60 *1000);
	}
	
	public static final Date getTomorrowClock(int hour)
	{
		Date tomorrow = getStepDate(new Date(), 1);
		Date tom = tomorrow;
		SimpleDateFormat sf = new SimpleDateFormat(dateFormater);
		try {
			tom = sf.parse(sf.format(tomorrow));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date(tom.getTime() + hour * 60 * 60 *1000);
	}
	
	public static final int calculationDateDiff(Date start, Date end){
		long diff = (end.getTime() - start.getTime())/1000;
		long days = diff / (24*3600);
		return (int)days;
	}
	
	public static void main(String[] args){
//		System.out.println(getTomorrowClock(0));
//		Date date2 = DateTimeTools.getDateByPattern("2009-11-12 22:33:45", DateTimeTools.dateTimeFormater);
//		int val = marginCalculations(date, date2);
//		System.out.println(val);
	}
}
